package main

import (
	"fmt"
	"log"
	"net"

	pb "gitee.com/wangjq4214/schedule/proto/leader"
	"google.golang.org/grpc"
)

type GRPCServe struct {
	server *Server
	port   int
}

func NewGRPCServe(server *Server, port int) *GRPCServe {
	return &GRPCServe{
		server: server,
		port:   port,
	}
}

func (g *GRPCServe) Lis() {
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	s := grpc.NewServer()
	pb.RegisterLeaderServeServer(s, g.server)

	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
